//
//  WMH_Main_ViewController.m
//  wmh.ios.v5
//
//  Created by AndLi on 14-8-29.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WMH_Main_Static_ViewController.h"

#define GetPadNavInfo 2
#define uploadErrorInfo 3

#define getFocusTitleTaskId 4
#define getFocusImageTaskId 5
#define SetCheckUpdateTaskId 6

@interface WMH_Main_Static_ViewController ()

@end

@implementation WMH_Main_Static_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if(IS_IPHONE_5){
            self = [super initWithNibName:@"WMH_Main_Static_ViewController5" bundle:nibBundleOrNil];
        }else{
            self = [super initWithNibName:@"WMH_Main_Static_ViewController" bundle:nibBundleOrNil];
        }

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"新点微门户V5";
    
    //上报错误日志
    [self startTask:@"EpointUploadMobileErrorLogTask" withParams:nil withTaskId:uploadErrorInfo];
    
    //设置焦点图高度
    focusViewHeight = 250;
    if(IS_IPHONE_5){
        focusViewHeight = 287;
    }
    
    //设置焦点图控件的一些属性
    self.myScrollView.contentSize = CGSizeMake(1280, focusViewHeight);
    self.myScrollView.delegate = self;
    self.pageControl.numberOfPages = 4;
    self.pageControl.currentPage = 0;
    
    getImgFailureTimes = 0;
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    
    //读取本地缓存
    [self getLocalHeadPic];
    
    //获取焦点图标题任务
    [self getFocusTitleTask];
    
    //检查更新
//    [self startTask:@"EpointCheckUpdateTask" withParams:nil withTaskId:SetCheckUpdateTaskId];
    
    [self setRightBarButtonItemWithTitle:@"设置" action:@selector(gotoSettingView)];
    
}

//系统设置
-(void)gotoSettingView{
    WMH_Setting_ViewController *view = [[WMH_Setting_ViewController alloc]init];
    [[self navigationController] pushViewController:view animated:YES];
}



#pragma mark 焦点图

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
    InfoListModel *m = [focusTitleLists objectAtIndex:self.pageControl.currentPage];
    self.focusTitleLabel.text = m.Title;
}

-(void)touchSV{
    
    InfoListModel *m = [focusTitleLists objectAtIndex:self.pageControl.currentPage];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.InfoID forKey:@"InfoID"];
    [params setObject:CateNum_HeadNews  forKey:@"CateNum"];
    [params setObject:@"0" forKey:@"IsNeedUrl"];
    WMH_NewsDetail_ViewController *view = [[WMH_NewsDetail_ViewController alloc]init];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}

//获取本地焦点图
-(void)getLocalHeadPic{
    NSString *key = [NSString stringWithFormat:@"%@%@%@%@",NEWSLIST,CateNum_HeadNews,@"4",@"0"];
    
    NSString *LastHeadNewsListXML = [DBFrameService getCacheValue:key];
    
    focusTitleLists = [Task_GetInfoList getLists:LastHeadNewsListXML];
    
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    if (focusTitleLists.count==4) {
        
        InfoListModel *m = [focusTitleLists objectAtIndex:self.pageControl.currentPage];
        self.focusTitleLabel.text = m.Title;
        
        for (int i=0; i<focusTitleLists.count; i++) {
            InfoListModel *hnm = [focusTitleLists objectAtIndex:i];
            NSData *d = [ud objectForKey:hnm.InfoID];
            MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, focusViewHeight)];
            ascrView.image = [UIImage imageWithData:d];
            ascrView.dele = self;
            [self.myScrollView addSubview:ascrView];
        }
    }
}


#pragma mark 刷新UI
-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];//关闭滚动条
    
    
    if (taskId==SetCheckUpdateTaskId) {
        [EpointUpdateService validateUpdateInfo:obj vc:self flag:1];
    }else{
        if (taskId==getFocusTitleTaskId) {
            [self dealFocusTitleData:obj];
        }
        else {
            [self dealFocusImageData:obj];
        }
    }
    
    
}

#pragma mark 获取焦点图标题任务
-(void)getFocusTitleTask
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"0"              forKey:@"CurrentPageIndex"];
    [params setObject:@"4"              forKey:@"PageSize"];
    [params setObject:CateNum_HeadNews  forKey:@"CateNum"];
    [params setObject:@"1"              forKey:@"IsHeadNews"];
    [params setObject:@""               forKey:@"Title"];
    [self startTask:@"Task_GetInfoList" withParams:params withTaskId:getFocusTitleTaskId];
}

-(void)dealFocusTitleData:(NSObject *)obj{
    NSMutableArray *bd = (NSMutableArray *)obj;
    if ([[bd objectAtIndex:0]isEqualToString:@"1"])
    {
        focusTitleLists = [bd objectAtIndex:2];
        InfoListModel *m = [focusTitleLists objectAtIndex:self.pageControl.currentPage];
        self.focusTitleLabel.text = m.Title;
        
        
//        //请求图片
        for (int i=0; i<focusTitleLists.count; i++) {
            //[self getFocusImageTask:[focusTitleLists objectAtIndex:i]];  //已废弃，采用URL加载图片
            m = [focusTitleLists objectAtIndex:i];
            MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(320 * i, 0, 320, focusViewHeight)];
            NSURL *imageUrl = [NSURL URLWithString:m.HeadNewsAttachUrl];
            //缓存
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            NSData *fileData = [NSData dataWithContentsOfURL:imageUrl];
            [ud setObject:fileData forKey:m.InfoID];
            ascrView.image = [UIImage imageWithData:fileData];
            ascrView.dele = self;
            [self.myScrollView addSubview:ascrView];
        }
        
    }
    
}

#pragma mark 获取焦点图图片任务
-(void)getFocusImageTask:(InfoListModel*)model
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:model forKey:@"HeadNewsModel"];
    [self startTask:@"Task_GetInfoHeadPic" withParams:params withTaskId:getFocusImageTaskId];
}

-(void)dealFocusImageData:(NSObject *)obj{
    NSMutableArray *bd = (NSMutableArray *)obj;
    if ([[bd objectAtIndex:1]isEqualToString:@"1"])
    {
        HeadPicModel *im = [bd objectAtIndex:2];
        int idx = 0;
        for (int i=0; i<focusTitleLists.count; i++)  {
            InfoListModel *m = [focusTitleLists objectAtIndex:i];
            if ([im.AttID isEqualToString:m.HeadNewsAttachGuid]) {
                idx = i;
                break;
            }
        }
        MyScrollView *ascrView = [[MyScrollView alloc] initWithFrame:CGRectMake(320 * idx, 0, 320, focusViewHeight)];
        ascrView.image = [UIImage imageWithData:im.fileData];
        ascrView.dele = self;
        [self.myScrollView addSubview:ascrView];
        
    }
    else
    {
        getImgFailureTimes++;
        NSLog(@"Failure Count：%i",getImgFailureTimes);
        if (getImgFailureTimes<3) {
            [self getFocusImageTask: [bd objectAtIndex:0]];
        }
    }
    
    
}

#pragma mark 栏目点击事件
-(IBAction)btn1Click:(id)sender{
    //跳转到新闻界面
    WMH_NewsList_ViewController *view = [[WMH_NewsList_ViewController alloc]init];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"新闻中心" forKey:@"title"];
    [params setObject:@"" forKey:@"categoryguid"];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}
-(IBAction)btn2Click:(id)sender{
    //跳转到新闻界面
    WMH_NewsUrlList_ViewController *view = [[WMH_NewsUrlList_ViewController alloc]init];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:@"新闻URL" forKey:@"title"];
    [params setObject:@"" forKey:@"categoryguid"];
    view.passParam = params;
    [self.navigationController pushViewController:view animated:YES];
}
-(IBAction)btn3Click:(id)sender{

}
-(IBAction)btn4Click:(id)sender{
}
-(IBAction)btn5Click:(id)sender{
}
-(IBAction)btn6Click:(id)sender{
}


@end
